class code2 {
    public boolean isValid(String s) {
        StringBuilder stack = new StringBuilder();

        char[] ss = s.toCharArray();

        HashMap<Character,Character> map = new HashMap<>();
        map.put('}','{');
        map.put(']','[');
        map.put(')','(');

        for(char x:ss) {
            //左
            if(map.containsValue(x)) {
                stack.append(x);
            } else {
                //右

                //匹配
                if(!stack.isEmpty() && stack.charAt(stack.length()-1) == map.get(x)) {
                    stack.deleteCharAt(stack.length()-1);
                } else {
                    //不匹配
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}